package com.mdxq.mall.sale.controller;

import cn.hutool.core.util.StrUtil;
import com.mdxq.mall.response.R;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.util.UpdateEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.mdxq.mall.sale.entity.HomeAdvertise;
import com.mdxq.mall.sale.service.HomeAdvertiseService;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Arrays;
import java.util.List;

/**
 * 首页轮播广告表 控制层。
 *
 * @author zdy
 * @since 2025-01-12
 */
@RestController
@RequestMapping("/api/v1/home/advertise")
public class HomeAdvertiseController {

    @Autowired
    private HomeAdvertiseService homeAdvertiseService;

    /**
     * 添加首页轮播广告表。
     *
     * @param homeAdvertise 首页轮播广告表
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
    @PostMapping("create")
    public R save(@RequestBody HomeAdvertise homeAdvertise) {
        return R.ok(homeAdvertiseService.save(homeAdvertise));
    }

    /**
     * 根据主键删除首页轮播广告表。
     *
     * @param ids 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @PostMapping("delete")
    public R remove(@RequestParam String ids) {
        List<String> idList = Arrays.asList(ids.split(","));
        return R.ok(homeAdvertiseService.removeByIds(idList));
    }

    /**
     * 根据主键更新首页轮播广告表。
     *
     * @param homeAdvertise 首页轮播广告表
     * @return {@code true} 更新成功，{@code false} 更新失败
     */
    @PostMapping("update")
    public R update(@RequestBody HomeAdvertise homeAdvertise) {
        return R.ok(homeAdvertiseService.updateById(homeAdvertise));
    }
    @PostMapping("update/status/{id}")
    public boolean updateStatus(@PathVariable Long id, @RequestParam Integer status) {
        HomeAdvertise homeAdvertise = UpdateEntity.of(HomeAdvertise.class,id);
        homeAdvertise.setStatus(status);
        return homeAdvertiseService.updateById(homeAdvertise);
    }
    /**
     * 查询所有首页轮播广告表。
     *
     * @return 所有数据
     */
    @GetMapping("list")
    public List<HomeAdvertise> list() {
        return homeAdvertiseService.list();
    }

    /**
     * 根据首页轮播广告表主键获取详细信息。
     *
     * @param id 首页轮播广告表主键
     * @return 首页轮播广告表详情
     */
    @GetMapping("getInfo/{id}")
    public R getInfo(@PathVariable Long id) {
        return R.ok(homeAdvertiseService.getById(id));
    }

    /**
     * 分页查询首页轮播广告表。
     *
     * @param page 分页对象
     * @return 分页对象
     */
    @GetMapping("page")
    public R page(Page<HomeAdvertise> page, String name, Integer type, LocalDate endTime) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (StrUtil.isNotBlank(name)) {
            queryWrapper.eq("name", name);
        }
        if (type != null) {
            queryWrapper.eq("type", type);
        }
        if (endTime != null) {
            queryWrapper.between("end_time",
                    LocalDateTime.of(endTime, LocalTime.MIN),
                    LocalDateTime.of(endTime, LocalTime.MAX));
        }
        return R.ok(homeAdvertiseService.page(page,queryWrapper));
    }

}
